package com.authorization.config;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chengyadong
 * @date 2023/11/29 10:37
 * @description 登录失败数据处理同时会将失败异常信息传递过来
 */
@Component
public class FailureHandlerCustomer implements AuthenticationFailureHandler {
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		response.setContentType("application/json;charset=UTF-8");
		Map parms = new HashMap();
		parms.put("code","1");
		parms.put("msg",exception.getMessage());
		response.getWriter().println(JSONObject.toJSON(parms));
	}
}
